package io.github.talelin.latticy.controller.v1;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.talelin.autoconfigure.exception.FailedException;
import io.github.talelin.autoconfigure.exception.ParameterException;
import io.github.talelin.latticy.model.AcceptDO;
import io.github.talelin.latticy.service.AcceptService;
import io.github.talelin.latticy.utils.ExcelUtils;
import io.github.talelin.latticy.utils.FtlUtils;
import io.github.talelin.latticy.vo.MaintenanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.service.MaintenanceService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Positive;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.github.talelin.latticy.dto.maintenance.CreateOrUpdateMaintenanceDTO;
import io.github.talelin.latticy.model.MaintenanceDO;

/**
 * @author vvboy
 */
@RestController
@RequestMapping("/v1/maintenance")
@Validated
public class MaintenanceController {

    @Autowired
    private MaintenanceService maintenanceService;
    @GetMapping("/{id}")
    public MaintenanceVo getMaintenance(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        MaintenanceVo maintenance = maintenanceService.getByIdVo(id);
        if (maintenance == null) {
            throw new NotFoundException("找不到", 10022);
        }
        return maintenance;
    }

    @GetMapping("")
    public List<MaintenanceDO> getMaintenances() {
        return maintenanceService.findAll();
    }


    @PostMapping("")
    public CreatedVO createMaintenance(@RequestBody @Validated CreateOrUpdateMaintenanceDTO validator) {
        maintenanceService.createMaintenance(validator);
        return new CreatedVO(12);
    }


    @PutMapping("/{id}")
    public UpdatedVO updateMaintenance(@PathVariable("id") @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdateMaintenanceDTO validator) {
        MaintenanceDO maintenance = maintenanceService.getById(id);
        if (maintenance == null) {
            throw new NotFoundException(10022);
        }
        maintenanceService.updateMaintenance(maintenance, validator);
        return new UpdatedVO(13);
    }


    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除", module = "")
    public DeletedVO deleteMaintenance(@PathVariable("id") @Positive(message = "{id.positive}") Integer id) {
        MaintenanceDO maintenance = maintenanceService.getById(id);
        if (maintenance == null) {
            throw new NotFoundException(10022);
        }
        maintenanceService.deleteById(maintenance.getId());
        return new DeletedVO(14);
    }

    @RequestMapping("exportAllExcel")
    public void exportAllExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") Integer id) throws IOException {
        request.getSession().setAttribute("token", "Y");
        Map<String, Object> map = new HashMap<String, Object>();
        String templateFileName = ClassUtils.getDefaultClassLoader().getResource("").getPath()
                + "static/file/maintenance.xls";
        String destFileName = "维修工作单.xls";
        try {
            MaintenanceDO maintenanceDO = maintenanceService.getById(id);
            maintenanceDO.setShelfStartString(DateUtil.format(maintenanceDO.getShelfStart(),"yyyy-MM-dd"));
            maintenanceDO.setShelfEndString(DateUtil.format(maintenanceDO.getShelfEnd(),"yyyy-MM-dd"));
            map.put("data", maintenanceDO);
            ExcelUtils.outputToExcel(map, templateFileName, destFileName, response, request);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @RequestMapping("exportAllWord")
    @CrossOrigin
    public void exportAllWord(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") Integer id) throws IOException {
        request.getSession().setAttribute("token", "Y");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            MaintenanceDO maintenanceDO = maintenanceService.getById(id);
            maintenanceDO.setOrderTimeString(DateUtil.format(maintenanceDO.getOrderTime(),"yyyy-MM-dd"));
            maintenanceDO.setShelfStartString(DateUtil.format(maintenanceDO.getShelfStart(),"yyyy-MM-dd"));
            maintenanceDO.setShelfEndString(DateUtil.format(maintenanceDO.getShelfEnd(),"yyyy-MM-dd"));
            map.put("data", maintenanceDO);
            FtlUtils.reportPeisOrgReservation(map,response,"maintenance.ftl");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @RequestMapping("exportAllPdf")
    @CrossOrigin
    public void exportAllPdf(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") Integer id) throws IOException {
        request.getSession().setAttribute("token", "Y");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            MaintenanceDO maintenanceDO = maintenanceService.getById(id);
            maintenanceDO.setOrderTimeString(DateUtil.format(maintenanceDO.getOrderTime(),"yyyy-MM-dd"));
            maintenanceDO.setShelfStartString(DateUtil.format(maintenanceDO.getShelfStart(),"yyyy-MM-dd"));
            maintenanceDO.setShelfEndString(DateUtil.format(maintenanceDO.getShelfEnd(),"yyyy-MM-dd"));
            map.put("data", maintenanceDO);
            FtlUtils.reportPeisOrgReservationPdf(map,response,"maintenance.ftl");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }


    @GetMapping("statCurMonth")
    public List<String> statCurMonth(){
        return maintenanceService.statCurMonth();
    }
}